package com.wutianyi.images;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BlurredImage extends JPanel
{

    /**
     * 
     */
    private static final long serialVersionUID = -8209506688722019341L;

    BufferedImage castle;
    BufferedImage filteredImage;
    Dimension size;

    public BlurredImage()
    {
        try
        {
            castle = ImageIO.read(this.getClass().getResourceAsStream("/banner.jpg"));
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        filteredImage = new BufferedImage(castle.getWidth(null), castle.getHeight(null), BufferedImage.TYPE_BYTE_GRAY);
        size = new Dimension();
        size.width = castle.getWidth(null);
        size.height = castle.getHeight(null);
        setPreferredSize(size);
        Graphics g = filteredImage.getGraphics();

        g.drawImage(castle, size.width + 10, size.height + 10, null);

        float[] blurKernel =
        { 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f };
        
        BufferedImageOp blur = new ConvolveOp(new Kernel(3, 3, blurKernel));
        castle = blur.filter(castle, new BufferedImage(castle.getWidth(), castle.getHeight(), castle.getType()));
        g.dispose();
        
    }
    
    public void paint(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(castle, null, 3, 3);
    }
    
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Blurred Image");
        frame.add(new BlurredImage());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}
